package hn.cch.feature.reflect;

import hn.cch.feature.reflect.entity.AbstractClass;
import hn.cch.feature.reflect.entity.ExtentsClass;
import hn.cch.feature.reflect.entity.MethodClass;
import org.junit.Test;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ReflectTest {

    public ReflectUtil reflectUtil = new ReflectUtil();


    /**
     * 测试泛型反射
     */
    @Test
    public void testGeneric() throws Exception {


        // 继承类型
        Class<ExtentsClass> extentsClass = ExtentsClass.class;
        System.out.println("ExtentsClass");
        Type genericSuperclass = extentsClass.getGenericSuperclass();
        reflectUtil.handleType(genericSuperclass);

        // System.out.println("Field");
        // Field field = extentsClass.getDeclaredField("clazz");
        // Type genericType = field.getGenericType();
        // reflectUtil.handleType(genericType);

        // 抽象类型
        Class<AbstractClass> abstractClass = AbstractClass.class;
        System.out.println("AbstractClass");
        TypeVariable<Class<AbstractClass>>[] typeParameters = abstractClass.getTypeParameters();
        for (TypeVariable typeVariable : typeParameters) {
            reflectUtil.handleTypeVariable(typeVariable);
        }


        Class<MethodClass> methodClass = MethodClass.class;
        System.out.println("MethodClass");
        Method[] methods = methodClass.getMethods();
        for (Method method : methods) {
            System.out.println("method:" + method.getName());
            Type[] genericParameterTypes = method.getGenericParameterTypes();
            for (Type type : genericParameterTypes) {
                reflectUtil.handleType(type);
            }
        }


        System.out.println();


        // Field[] fieldArray = extentsClass.getDeclaredFields();
        // for (Field field:fieldArray){
        //     Type genericType = field.getGenericType();
        //     reflectUtil.handleType(genericType);
        //
        // }

    }


    @Test
    public void testElement() {
        // 集合元素
        List<String> stringList = new ArrayList<>(Arrays.asList("hello", "world"));
        Class<? extends List> listClass = stringList.getClass();


        System.out.println();
    }
}


